<?php
namespace App\Models;

use Illuminate\Support\Facades\Input;
use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission{

    protected $guarded = [];

    public function scopeParent_id($query, $parent_id = -1)
    {
        $parent_id = $parent_id >= 0 ? $parent_id : Input::get('parent_id', -1);
        if ($parent_id >= 0) {
            return $query->where('parent_id', '=', $parent_id);
        }
        return $query;
    }

    public function scopeDisplay_name($query, $display_name = '')
    {
        $display_name = $display_name != '' ? : Input::get('display_name', '');
        if (!empty($display_name)) {
            return $query->where('display_name', 'like', "%{$display_name}%");
        }
        return $query;
    }

    public function scopeName($query, $name = '')
    {
        $name = $name != '' ? : Input::get('name', '');
        if (!empty($name)) {
            return $query->where('name', 'like', "%{$name}%");
        }
        return $query;
    }

    public function parent()
    {
        return $this->belongsTo('App\Models\Permission', 'parent_id');
    }

    public function son()
    {
        return $this->hasMany('App\Models\Permission', 'parent_id');
    }

    public function roles()
    {
        return $this->belongsToMany('App\Models\Role');
    }
}